home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / startup.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  8.7 KB  |  239 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. #define EXECBASE        (*(struct ExecBase **)4)
  33. #define aprintf         Printf
  34.  
  35. __asm ULONG main (register __a0 struct WBStartup *);
  36.  
  37. static char Template[] = TEMPLATE;
  38. static char WBTemplate[] = WBTEMPLATE;
  39.  
  40. extern struct  ExecBase    *SysBase;
  41.  
  42. static struct  RDArgs      *rdargs;
  43. static struct  RDArgs      *myRDArgs;
  44.        struct  Args        opts;
  45.  
  46. struct Process    *proc;
  47. struct WBStartup  *msg;
  48.  
  49. ULONG __saveds startup (void) {
  50.    ULONG rc;
  51.    BYTE  oldtaskpri = 0;
  52.    BPTR  oldinput = (BPTR)NULL, newinput = (BPTR)NULL, oldoutput = (BPTR)NULL;
  53.  
  54.    SysBase = EXECBASE;
  55.    rc = RETURN_FAIL;
  56.    proc = (struct Process *) FindTask (NULL);
  57.  
  58.    if (!(proc->pr_CLI)) {
  59.       WaitPort(&proc->pr_MsgPort);
  60.       msg = (struct WBStartup *) GetMsg (&proc->pr_MsgPort);
  61.    }
  62.  
  63.    DOSBase = (struct DosLibrary *) OpenLibrary (DOSNAME, 37);
  64.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 37);
  65.    UtilityBase = OpenLibrary (UTILITYNAME, 37);
  66.  
  67.    if (DOSBase && IntuitionBase && UtilityBase) {
  68.  
  69.       if (msg) {
  70.          struct   Library  *IconBase;
  71.          struct   WBArg    *wbArg;
  72.  
  73.          /*
  74.           * Started from Workbench so do icon magic...
  75.           *
  76.           * What we will do here is try all of the tooltypes
  77.           * in the icon and keep only those which do not cause
  78.           * errors in the RDArgs.
  79.           */
  80.          if ((wbArg = msg->sm_ArgList) && (IconBase = OpenLibrary (ICONNAME,0))) {
  81.             struct   DiskObject  *diskObj;
  82.                      BPTR        tmplock;
  83.  
  84.             /*
  85.              * Use project icon if it is there...
  86.              */
  87.             if (msg->sm_NumArgs > 1) wbArg++;
  88.  
  89.             tmplock = CurrentDir (wbArg->wa_Lock);
  90.             if (diskObj = GetDiskObject (wbArg->wa_Name)) {
  91.                char  **ToolTypes;
  92.  
  93.                if (ToolTypes = diskObj->do_ToolTypes) {
  94.                   char  *TotalString;
  95.                   ULONG totalSize = 3;
  96.  
  97.                   while (*ToolTypes) {
  98.                      totalSize += strlen(*ToolTypes)+1;
  99.                      ToolTypes++;
  100.                   }
  101.  
  102.                   if (TotalString = AllocVec (totalSize,MEMF_PUBLIC)) {
  103.                   char  *CurrentPos = TotalString;
  104.                   ULONG currentLength;
  105.  
  106.                      ToolTypes = diskObj->do_ToolTypes;
  107.                      do {
  108.                         *CurrentPos='\0';
  109.                         if (*ToolTypes) strcpy (CurrentPos,*ToolTypes);
  110.                         currentLength=strlen(CurrentPos);
  111.                         CurrentPos[currentLength+0]='\n';
  112.                         CurrentPos[currentLength+1]='\0';
  113.  
  114.                         if (rdargs) FreeArgs(rdargs);
  115.                         memset ((char *) &opts, 0, sizeof(opts));
  116.  
  117.                         if (myRDArgs) FreeDosObject (DOS_RDARGS,myRDArgs);
  118.                         if (myRDArgs = AllocDosObject (DOS_RDARGS,NULL)) {
  119.                            myRDArgs->RDA_Source.CS_Buffer = TotalString;
  120.                            myRDArgs->RDA_Source.CS_Length = strlen(TotalString);
  121.  
  122.                            if (rdargs = ReadArgs (WBTemplate, (LONG *) &opts, myRDArgs)) {
  123.                               CurrentPos[currentLength]=' ';
  124.                               CurrentPos+=currentLength+1;
  125.                            }
  126.                         }
  127.                      } while (*ToolTypes++);
  128.                      FreeVec (TotalString);
  129.                   }
  130.                }
  131.                FreeDiskObject(diskObj);
  132.             }
  133.  
  134.             CurrentDir (tmplock);
  135.             CloseLibrary (IconBase);
  136.          }
  137.  
  138.          if (newinput = Open ("CON:0/12/400/100/Scout/AUTO/CLOSE/WAIT", MODE_READWRITE)) {
  139.             oldinput = SelectInput (newinput);
  140.             oldoutput = SelectOutput (newinput);
  141.          }
  142.          rc = RETURN_OK;
  143.       } else {
  144.          /*
  145.           * Started from CLI so do standard ReadArgs
  146.           */
  147.          if (!(rdargs = ReadArgs (Template, (LONG *) &opts, NULL)))
  148.             PrintFault(IoErr(),NULL);
  149.          else if (SetSignal(0,0) & SIGBREAKF_CTRL_C)
  150.             PrintFault(ERROR_BREAK,NULL);
  151.          else
  152.             rc = RETURN_OK;
  153.       }
  154.  
  155.       if ((opts.ToolPri) && ((*opts.ToolPri < -128) || (*opts.ToolPri > 127))) {
  156.          Write (Output(),"ToolPri is not in range (-128 - 127).\n",38);
  157.       } else if ((opts.CpuDisplay) && ((*opts.CpuDisplay < 1) || (*opts.CpuDisplay > 2))) {
  158.          Write (Output(),"CpuDisplay is not in range (1 - 2).\n",36);
  159.       } else if ((opts.SortLibrariesType) && ((*opts.SortLibrariesType < 1) || (*opts.SortLibrariesType > 2))) {
  160.          Write (Output(),"SortLibrariesType is not in range (1 - 2).\n",43);
  161.       } else if ((opts.SortDevicesType) && ((*opts.SortDevicesType < 1) || (*opts.SortDevicesType > 2))) {
  162.          Write (Output(),"SortDevicesType is not in range (1 - 2).\n",41);
  163.       } else if ((opts.SortResourcesType) && ((*opts.SortResourcesType < 1) || (*opts.SortResourcesType > 2))) {
  164.          Write (Output(),"SortResourcesType is not in range (1 - 2).\n",43);
  165.       } else if ((opts.SortTasksType) && ((*opts.SortTasksType < 1) || (*opts.SortTasksType > 2))) {
  166.          Write (Output(),"SortTasksType is not in range (1 - 2).\n",39);
  167.       } else if ((opts.SortPortsType) && ((*opts.SortPortsType < 1) || (*opts.SortPortsType > 2))) {
  168.          Write (Output(),"SortPortsType is not in range (1 - 2).\n",39);
  169.       } else if ((opts.SortCommandsType) && ((*opts.SortCommandsType < 1) || (*opts.SortCommandsType > 2))) {
  170.          Write (Output(),"SortCommandsType is not in range (1 - 2).\n",42);
  171.       } else if ((opts.SortAssignsType) && ((*opts.SortAssignsType < 1) || (*opts.SortAssignsType > 2))) {
  172.          Write (Output(),"SortAssignsType is not in range (1 - 2).\n",41);
  173.       } else if ((opts.SortLocksType) && ((*opts.SortLocksType < 1) || (*opts.SortLocksType > 2))) {
  174.          Write (Output(),"SortLocksType is not in range (1 - 2).\n",39);
  175.       } else if ((opts.SortCommoditiesType) && ((*opts.SortCommoditiesType < 1) || (*opts.SortCommoditiesType > 2))) {
  176.          Write (Output(),"SortCommoditiesType is not in range (1 - 2).\n",45);
  177.       } else if ((opts.SortScreenmodeType) && ((*opts.SortScreenmodeType < 1) || (*opts.SortScreenmodeType > 2))) {
  178.          Write (Output(),"SortScreenmodeType is not in range (1 - 2).\n",44);
  179.       } else if ((opts.SortClassesType) && ((*opts.SortClassesType < 1) || (*opts.SortClassesType > 2))) {
  180.          Write (Output(),"SortClassesType is not in range (1 - 2).\n",44);
  181.       } else {       
  182.          if (rc == RETURN_OK) {
  183.             /*
  184.              * Set program priority if requested
  185.              */
  186.             if (opts.ToolPri) {
  187.                oldtaskpri = SetTaskPri ((struct Task *) proc,*opts.ToolPri);
  188.             }
  189.  
  190.             /*
  191.              * Call the main program
  192.              */
  193.             rc = main (msg);
  194.          }
  195.  
  196.          /*
  197.           * Reset program priority if necessary
  198.           */
  199.          if (opts.ToolPri) {
  200.             SetTaskPri ((struct Task *) proc,(LONG) oldtaskpri);
  201.          }
  202.       }
  203.       if (rdargs) FreeArgs (rdargs);
  204.       if (myRDArgs) FreeDosObject (DOS_RDARGS,myRDArgs);
  205.    
  206.    } else if (!msg) {
  207.       if (!DOSBase) DOSBase = (struct DosLibrary *) OpenLibrary(DOSNAME,0);
  208.       if (DOSBase) {
  209.          Write (Output(),"Requires Kickstart 2.04 (37.175) or later.\n",43);
  210.       }
  211.    }
  212.  
  213.  
  214.    if ((msg) && (newinput)) {
  215.  
  216.       SelectInput (oldinput);
  217.       SelectOutput (oldoutput);
  218.       Close (newinput);
  219.    }
  220.  
  221.    /*
  222.     * We do this in order to not crash under 1.3...
  223.     * We do not run under 1.3, but that is another issue.
  224.     */
  225.    if (UtilityBase)  CloseLibrary (UtilityBase);
  226.    if (IntuitionBase)  CloseLibrary ((struct Library *) IntuitionBase);
  227.    if (DOSBase)  CloseLibrary ((struct Library *) DOSBase);
  228.  
  229.    if (msg) {
  230.  
  231.       Forbid();
  232.       ReplyMsg ((struct Message *) msg);
  233.    }
  234.    return (rc);
  235. }
  236.  
  237. void __stdargs _XCEXIT (long mist) {
  238. }
  239.